//
//  QKUIKitCtrl.m
//  testTool
//
//  Created by Su on 2018/12/30.
//  Copyright © 2018 com.qiankun. All rights reserved.
//

#import "QKUIKitCtrl.h"

//可计算宏


@interface QKUIKitCtrl ()<UIScrollViewDelegate>
{
    
}
@property(nonatomic,strong)UIWindow *statusBarWindows;
@property(nonatomic,strong)NSString *strongStr;

@property(strong,nonatomic)UICollectionView *collectionView;

//测试layer
@property(strong,nonatomic)CALayer * testLayer;
@property(strong,nonatomic)UIImageView * testView;


@end

@implementation QKUIKitCtrl

-(void)injected{
/*
 iOS中的事件分为3大类型
     1. 触摸类型
     2. 加速器事件
     3. 远程控制事件
 
  响应者对象, 继承自UIResponder的对象. UIApplication UIViewController UIView 等等
 一个手指触摸屏幕的时候, 会创建一个与触摸点相关的的UITOuch对象,包含,位置,时间,阶段等;这个对象信息跟shou触摸实时保持更新;
 手指离开屏幕后UItouch这个对象会自动销毁;
 
 UIEvent. 事件的发生和处理
 1. 发生一个触摸事件后. 系统会将事件加入到有UIApplication管理的事件队列中, UIApplication从事件队列中取出最前面的事件, 并将事件分发下去处理.(keywindow)
 2. keyWindow 会根据视图层级找到一个合适的视图来处理触摸事件.
 3.
 4. 找到合适的视图后, 调用视图控件的touchesshi来处理
 
 */

//    [self testLayer];
//    [self  test3DTrans];
    [self  viewDidLoad];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //0.543538547 980/1803   width height
    
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    UITouch *touch = touches.anyObject;
//    CGPoint location = [touch locationInView:self.view];
//    self.testLayer.position = location;
//    CGFloat scale = (arc4random_uniform(5) + 1) / 10.0 + 0.5;
//    //    self.myLayer.transform = CATransform3DMakeScale(scale, scale, 0);
//        [self.testLayer setValue:@(scale) forKeyPath:@"transform.scale"];
//
//        // 3. 旋转69
//        CGFloat rotate =69;
//    //    self.myLayer.transform = CATransform3DMakeRotation(rotate, 0, 0, 1);
//        [self.testLayer setValue:@(rotate) forKeyPath:@"transform.rotation.y"];
//
//        // 4. 透明度
//        CGFloat alpha = (arc4random_uniform(5) + 1) / 10.0 + 0.5;
//    //    self.myLayer.opacity = alpha;
//        [self.testLayer setValue:@(alpha) forKeyPath:@"opacity"];
//
//        // 5. 设置圆角半径
//        CGFloat r = arc4random_uniform(self.testLayer.bounds.size.width * 0.5);
//        self.testLayer.cornerRadius = r;
//
//        // 6. 设置边线
//        self.testLayer.borderColor = UIColor.qmui_randomColor.CGColor;
//        self.testLayer.borderWidth = 3.0;
    
}

-(CALayer *)testLayer{
    if (!_testLayer) {
        _testLayer = [CALayer new];
        _testLayer.bounds = CGRectMake(0, 0, 100, 100);
        _testLayer.position = self.view.center;
        _testLayer.backgroundColor = UIColor.redColor.CGColor;
        _testLayer.opacity = 1.0;
        _testLayer.contents = (__bridge id)([UIImage imageNamed:@"IMG_2523"].CGImage);
        _testLayer.masksToBounds = YES;
        [self.view.layer addSublayer:_testLayer];
        
    }
    return _testLayer;
}

-(void)test3DTrans
{
    
    self.testView = nil;
    [self.testView removeFromSuperview];
    
    self.testView = [UIImageView new];
    self.testView.image = [UIImage imageNamed:@"IMG_2523"];
    self.testView.layer.contentsRect = CGRectMake(0, 0, 1, 0.5);
    
    UIImageView *tempImg = [UIImageView new];
    tempImg.image = [UIImage imageNamed:@"IMG_2523"];
    tempImg.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5);
    
    
    
    
    [self.view addSubview:self.testView];
    [self.view addSubview:tempImg];
    
    
    
    
    [self.testView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view);
        make.height.mas_equalTo(204);
        make.width.mas_equalTo(204);
    }];
    
    [tempImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.testView.mas_bottom);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(204);
        make.width.mas_equalTo(204);
    }];
  
    
}



-(void)testCABasicAnimAation{
    //CABasicAnimation 基础动画
    
    CABasicAnimation *position = [CABasicAnimation animationWithKeyPath:@"position.y"];
    position.fromValue = @(self.testView.center.y);
    position.toValue = @(self.testView.center.y - 30);
    
    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    transformAnima.fromValue = @(0);
    transformAnima.toValue = @(M_PI);
    
    CAAnimationGroup *anim  = [CAAnimationGroup animation];
    anim.removedOnCompletion = NO;
    anim.fillMode = @"forwards";
    anim.animations = @[position,transformAnima];
}

-(void)testHeartJumpAnim{
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    anim.toValue = @0;
    anim.repeatCount = MAXFLOAT;
    anim.duration = 1.0;
    anim.autoreverses = YES;
    [self.testView.layer addAnimation:anim forKey:@"A"];
}
//CAKeyframeAnimation
-(void)TestCAKeyframeAnimation{
    //CAKeyframeAnimation 基础动画
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    // 指定 path
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(120, 120)];
    [path addLineToPoint:CGPointMake(300, 120)];
    [path addLineToPoint:CGPointMake(300, 340)];
    anim.duration = 2;
    anim.path = path.CGPath;
    
    //  指定values
    //    anim.duration = 1;
    //    anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))];
    //    anim.repeatCount = MAXFLOAT;
    //    anim.autoreverses = YES;
    [self.testView.layer addAnimation:anim forKey:nil];
}
//转场动画
-(void)CATransitionAnimation
{
    
//    NSArray *arr = [NSArray arrayWithObjects:@"fade",@"moveIn",@"push", @"reveal", nil];
//    _i ++;
//    if (_i == 4) {_i = 1;}
//    self.testView.image = [UIImage imageNamed:[NSString stringWithFormat:@"0%d",_i]];
//    CATransition *transition = [CATransition animation];
//    transition.duration = 1;
//    transition.type = @"cameraIrisHollow";
//    [self.testView.layer addAnimation:transition forKey:@"Anim"];
    
    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = @"cameraIrisHollow";
    transition.subtype = @"alignedCube";
    [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:0];
    [self.view.layer addAnimation:transition forKey:@"Anim"];
    
    UIViewController *two = UIViewController.new;
    two.view.backgroundColor = UIColor.redColor;
    [self.navigationController pushViewController:two animated:YES];
    [self.navigationController.view.layer addAnimation:transition forKey:@"anim"];
    
}



-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%@",touches);
   
}



//layer 相关
//-(void)testLayer
//{
//    UIView *view = UIView.new;
//    //设置阴影颜色
//    view.layer.shadowColor = UIColor.redColor.CGColor;
//    //设置不透明度
//    view.layer.shadowOpacity = 1.0;
//    //设置偏移量
//    self.view.layer.shadowOffset  = CGSizeMake(-30 , -30);
//    // 设置阴影模糊半径
//    self.view.layer.shadowRadius = 0.4;
//    //设置边框, 边框宽度w是往里走的;
//    self.view.layer.borderWidth = 3;
//    self.view.layer.borderColor = UIColor.purpleColor.CGColor;
//    //    view.layer.contents
//    
//}



-(void)testAFN{
    AFHTTPSessionManager *man = [AFHTTPSessionManager  manager];
  
    [man GET:[@"http://app.pppcar.com/v2.1/pp/search?currentPage=1&pageSize=20&full_search=1&keyWord=轮毂" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
      
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        //        // NSData转为NSString
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
      
        NSLog(@"%@",jsonStr);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    
}


/**
 通过系统提供的API创建实例 以及实现 相关方法. 后台可以动态传递参数

 */
-(void)testReflection{
    
    NSDictionary *confParams = @{
                                 @"className":@"QKTestReflectCtrl",
                                 @"propertys":@{@"name":@"suqiankun",@"age":@"23"},
                                 @"method":@"addButton"
                                 };
    
    Class class = NSClassFromString(confParams[@"className"]);
    UIViewController *ctrl = [[class alloc] init];
    NSDictionary*params = confParams[@"propertys"];
    [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([ctrl respondsToSelector:NSSelectorFromString(key)]) {
            [ctrl setValue:obj forKey:key];
        }
    }];
    [self.navigationController pushViewController:ctrl animated:YES];
    SEL selector = NSSelectorFromString(confParams[@"method"]);
    [ctrl performSelector:selector];
    
    
    
}

-(void)runLoop{
    /*
     常用两种模式
     
     UIDefaultRunLoopMode
     
     UITrackingRunLoopMode
     
     //两种模式都可以监听, 如果我们
     UNSRUnLoopCommentModel
     
     source0: 系统事件
     source1:
     
     
     
     */
    NSLog(@"%@",[NSRunLoop currentRunLoop]);
    
    //监听runloop
    CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        NSLog(@"---监听runloop----%lu",activity);
    });
    CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
    
    CFRelease(observer);
}
  

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetToShow = 200.0;//滑动多少就完全显示
    
    CGFloat alpha = 1 - (offsetToShow - scrollView.contentOffset.y) / offsetToShow;
    self.navigationController.navigationBar.barTintColor = UIColor.redColor;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = alpha;

}


-(void)backBlock{

    [self func:^(NSString * _Nonnull temp, UIColor * _Nonnull color) {
        self.view.backgroundColor = color;
    }];
    
}
-(void)func:(giveName) block{
    block(@"name",[UIColor redColor]);
}

- (void)makeFoolWithParams:(NSString *)name block:(void (^)(NSString * temp))callback
{
    NSLog(@"makeFool | callback.");
    callback([NSString stringWithFormat:@"This is a block parameter.%@",name]);
}

@end
